package cn.cloudmenu.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import cn.cloudmenu.android.Global;
import cn.cloudmenu.android.R;

/** 程序启动画面。 */
public class Splash extends Activity {
	private final int SPLASH_DISPLAY_LENGHT = 3000; // 延迟三秒

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		initGlobal();
		new Handler().postDelayed(new Runnable() {
			public void run() {
				if (neverShowAgain()) {// 如果用户已经设置不再显示帮助，则直接打开背单词
					Intent mainIntent = new Intent(Splash.this, CloudMenu.class);
					Splash.this.startActivity(mainIntent);
				} else {// 否则，显示帮助
					Intent beginIntent = new Intent(Splash.this, GuideBegin.class);
					Splash.this.startActivity(beginIntent);
				}
				Splash.this.finish();
			}

		}, SPLASH_DISPLAY_LENGHT);
	}

	private boolean neverShowAgain() {
		SharedPreferences settings = getSharedPreferences(GuideBegin.class.getName(), MODE_PRIVATE);
		boolean neverShowAgain = settings.getBoolean("neverShowAgain", false);
		// System.out.println("BeginIWords.neverShowAgain() neverShowAgain = " +
		// neverShowAgain);
		return neverShowAgain;
	}
	
	/**
	 * 初始化全局变量 实际工作中这个方法中serverVersion从服务器端获取，最好在启动画面的activity中执行
	 */
	public void initGlobal() {
		try {
			Global.localVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; // 设置本地版本号
			Global.serverVersion = 1;// 默认是1
			Global.downloadFromURL = getApplicationContext().getResources().getString(R.string.get_apk);
			//String href = getApplicationContext().getResources().getString(R.string.get_version);
			//HttpPost request = new HttpPost(href);
			//JSONObject jSONObject = ServerAccessor.access(this, request);
			//Global.serverVersion = jSONObject.getInt("version");
			// String info = jSONObject.getInt("info");
			System.out.println("Splash.java initGlobal localVersion = "+Global.localVersion+" serverVersion = "+Global.serverVersion);
		} catch (Exception e) {
			//ex.printStackTrace();
			this.displayMessage(e.getMessage());
		}
	}
	private void displayMessage(String message){
		Toast t1 = Toast.makeText(this, message, Toast.LENGTH_LONG);
		t1.show();
	}	
}